package com.yanqu.road.utils.date;

import com.yanqu.road.utils.TimeHelper;
import org.apache.commons.lang.time.DateUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.temporal.TemporalAdjusters;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

public class DateHelper {

    private static final Logger log = LogManager.getLogger(DateHelper.class);

    private static long nextMonthFirstDayTime;
    private static long currentMonthFirstDayTime;
    /**
     * 一周的秒表示
     */
    public static final long WEEK_SECONDS = 604800;
    /**
     * 一天的秒表示
     */
    public static final long DAY_SECONDS = 86400;
    /**
     * 一天的毫秒表示
     */
    public static final long DAY_MILLIONS = 86400000;
    /**
     * 一小时的秒表示
     */
    public static final long HOUR_SECONDS = 3600;
    /**
     * 一小时的毫秒表示
     */
    public static final long HOUR_MILLIONS = 3600000;
    /**
     * 一分钟的毫秒表示
     */
    public static final long MINUTE_MILLIONS = 60000;
    /**
     * 一分钟的秒表示
     */
    public static final long MINUTE_SECONDS = 60;
    /**
     * 一秒的毫秒表示
     */
    public static final long SECOND_MILLIONS = 1000;

    public static final String YMDHMS = "yyyy-MM-dd HH:mm:ss";
    public static final String YMDHMSS = "yyyy-MM-dd HH:mm:ss SSS";
    public static final String YMD = "yyyy/M/d";
    public static final String YYMMDD = "yyyyMMdd";
    public static final String YMD_BAR= "yyyy-MM-dd";

    /**
     * 取0点时间戳 （ms）
     */
    public static long getTodayZeroTimeStamp() {
        long nowTime = System.currentTimeMillis();
        return getZeroTimeStamp(nowTime);
    }

    /**
     * 取0点时间戳 （s）
     */
    public static int getTodayZeroTime() {
        long nowTime = System.currentTimeMillis();
        return (int)(getZeroTimeStamp(nowTime) / 1000);
    }



    /**
     * 以自然时间即0点区别
     * @param date      时间
     */
    public static boolean isToday(Date date) {
        if (date == null) {
            return false;
        }
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        Date end = cal.getTime();
        cal.add(Calendar.MILLISECOND, -1);
        cal.add(Calendar.DAY_OF_MONTH, -1);
        Date start = cal.getTime();
        return ((date.after(start)) && (date.before(end)));
    }

    public static String date2String(Date theDate) {
        SimpleDateFormat sdf = TimeHelper.getSimpleDateFormat(YMDHMS);
        return date2String(theDate, sdf);
    }

    public static String date2String(Date theDate, String datePattern) {
        if (theDate == null) {
            return "";
        }

        DateFormat format = TimeHelper.getSimpleDateFormat(datePattern);
        try {
            return format.format(theDate);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String date2String(Date theDate, SimpleDateFormat pattern) {
        if (theDate == null) {
            return "";
        }
        try {
            return pattern.format(theDate);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }



    public static int toSecond(long millis) {
        long second = millis / 1000L;
        return (int) second;
    }


    public static Date add(Date theDate, int addHours, int addMinutes, int addSecond) {
        if (theDate == null) {
            return null;
        }

        Calendar cal = Calendar.getInstance();
        cal.setTime(theDate);

        cal.add(Calendar.HOUR_OF_DAY, addHours);
        cal.add(Calendar.MINUTE, addMinutes);
        cal.add(Calendar.SECOND, addSecond);

        return cal.getTime();
    }



    /**
     * 本日期的零点时刻
     * @param theDate    时间
     */
    public static Date getDate0AM(Date theDate) {
        if (theDate == null) {
            return null;
        }

        Calendar cal = Calendar.getInstance();
        cal.setTime(theDate);
        return new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH))
                .getTime();
    }



    /**
     * 获得结束时间是从开始时间的第几天
     * @param startDate     开始
     * @param endDate       结束
     */
    public static int calc2DateTDOADays(Date startDate, Date endDate) {
        if ((startDate == null) || (endDate == null)) {
            return 0;
        }
        Date startDate0AM = getDate0AM(startDate);
        Date endDate0AM = getDate0AM(endDate);
        long v1 = startDate0AM.getTime() - endDate0AM.getTime();
        BigDecimal bd1 = new BigDecimal(v1);
        BigDecimal bd2 = new BigDecimal(DAY_MILLIONS);
        return Math.abs((int) bd1.divide(bd2, 0, 0).doubleValue()) + 1;
    }

    public static Date add(int addDay, boolean to0AM) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_MONTH, addDay);
        Date time = calendar.getTime();
        return ((to0AM) ? getDate0AM(time) : time);
    }

    /**
     * 返回当前时间的秒数，单位：秒
     */
    public static int getCurrentSecond() {
        return toSecond(System.currentTimeMillis());
    }

    /**
     * 返回当前毫秒时间，单位：毫秒
     */
    public static long getCurrentTime() {
        return System.currentTimeMillis();
    }

    public static Date getCurrentDate() {
        return new Date();
    }


    /**
     * 获取当前时间的月份
     */
    public static int getMonth() {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.MONTH) + 1;
    }

    /**
     * 获取当前时间的上一个月份
     */
    public static int getForwardMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -1);
        return calendar.get(Calendar.MONTH) + 1;
    }

    /**
     * 获取当前时间的月份第一天0点的时间 秒
     */
    public static long getMonthFirstDayZeroTime() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH),
                calendar.getActualMinimum(calendar.DAY_OF_MONTH), 0, 0, 0);
        return calendar.getTimeInMillis() / 1000;
    }

    /**
     * 获取当前时间的下一个月份第一天0点的时间  秒
     */
    public static long getNextMonthFirstDayZeroTime() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH),
                calendar.getActualMaximum(calendar.DAY_OF_MONTH), 23, 59, 59);
        return calendar.getTimeInMillis() / 1000 + 1;
    }

    /**
     * 获取yyyy/M/d格式日期
     *
     * @param time 秒
     */
    public static String getPyyyyMdDateString(long time) {
        SimpleDateFormat sdf = TimeHelper.getSimpleDateFormat(YMD);
        return sdf.format(time * 1000);
    }

    /**
     * 获取yyyy/M/d格式日期
     * @param time 秒
     */
    public static String getPyyyyMMddDateString(long time) {
        SimpleDateFormat sdf = TimeHelper.getSimpleDateFormat(YYMMDD);
        return sdf.format(time * 1000);
    }

    /**
     * 根据当前日期返回 yyyy-MM-dd HH:mm:ss 格式字符串
     */
    public static String getCurrentDateTimeString() {
        SimpleDateFormat sdf = TimeHelper.getSimpleDateFormat(YMDHMS);
        return sdf.format(Calendar.getInstance().getTime());
    }

    /**
     * 根据日期返回 yyyy-MM-dd HH:mm:ss 格式字符串
     */
    public static String getCurrentDateTimeString(long currDate) {
        SimpleDateFormat sdf = TimeHelper.getSimpleDateFormat(YMDHMS);
        return sdf.format(currDate);
    }

    /**
     * 根据 yyyy-MM-dd HH:mm:ss 格式字符串返回日期
     */
    public static Date getString2Date(String dateStr) {
        Date date = null;
        try {
            SimpleDateFormat sdf = TimeHelper.getSimpleDateFormat(YMDHMS);
            date = sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }





    /**
     * 获得当天任意时间
     * @param hour        时
     * @param minute      分
     * @param second      秒
     */
    public static Date getDate(int hour, int minute, int second) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, hour);
        cal.set(Calendar.SECOND, second);
        cal.set(Calendar.MINUTE, minute);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    /**
     * 获得指定时间的任意时间
     * @param hour        时
     * @param minute      分
     * @param second      秒
     */
    public static long getDateForTimeByTime(long time, int hour, int minute, int second) {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(time);
        cal.set(Calendar.HOUR_OF_DAY, hour);
        cal.set(Calendar.SECOND, second);
        cal.set(Calendar.MINUTE, minute);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime().getTime();
    }



    /**
     * 获得当天任意时间
     * @param hour        时
     * @param minute      分
     * @param second      秒
     */
    public static long getDateForTime(int hour, int minute, int second) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, hour);
        cal.set(Calendar.SECOND, second);
        cal.set(Calendar.MINUTE, minute);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime().getTime();
    }

    /**
     * 获得当天任意时间
     * @param hh 12
     * @return
     */
    public static final Date getDateByHours(int hh) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, hh);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    /**
     * 获得当前小时数 (24小时制)
     */
    public static int getHours() {
        Calendar rightNow = Calendar.getInstance();
        return rightNow.get(Calendar.HOUR_OF_DAY);
    }
    //
    public static int getTimeStampHours(long timestamp) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTimeInMillis(timestamp);
        int hour = cal1.get(Calendar.HOUR_OF_DAY);
        return hour;
    }


    /**
     * 是否是整点时间 分钟为0
     */
    public static boolean isPoint() {
        Calendar rightNow = Calendar.getInstance();
        return rightNow.get(Calendar.MINUTE) == 0;
    }




    public static boolean isSameWeek(Date date1, Date date2) {
        if (date1 == null || date2 == null) {
            return false;
        }

        long weekStartTime1 = DateHelper.getThisWeekStartTime(date1.getTime() / 1000, 1);
        long weekStartTime2 = DateHelper.getThisWeekStartTime(date2.getTime() / 1000, 1);
        return weekStartTime1 == weekStartTime2;
//        // 0.先把Date类型的对象转换Calendar类型的对象
//        Calendar todayCal = Calendar.getInstance();
//        Calendar dateCal = Calendar.getInstance();
//
//        todayCal.setTime(DateUtils.addDays(date1,-1));
//        dateCal.setTime(DateUtils.addDays(date2,-1));
//
//        int todayYear = todayCal.get(Calendar.YEAR);
//        int dateYear = dateCal.get(Calendar.YEAR);
//        if (todayYear != dateYear) {
//            return false;
//        }
//        // 1.比较当前日期在年份中的周数是否相同
//        return todayCal.get(Calendar.WEEK_OF_YEAR) == dateCal.get(Calendar.WEEK_OF_YEAR);
    }

    /**
     * 是否处于同一周
     *
     * @param time1 单位秒
     * @param time2 单位秒
     */
    public static boolean isSameWeek(long time1, long time2) {
        return isSameWeekByMillis(time1 * 1000, time2 * 1000);
    }

    /**
     * 是否处于同一周
     *
     * @param time1 单位毫秒
     * @param time2 单位毫秒
     */
    public static boolean isSameWeekByMillis(long time1, long time2) {
        long weekStartTime1 = DateHelper.getThisWeekStartTime(time1 / 1000, 1);
        long weekStartTime2 = DateHelper.getThisWeekStartTime(time2 / 1000, 1);
        return weekStartTime1 == weekStartTime2;

//        return isSameWeek(cal1.getTime(), cal2.getTime());
    }


    /**Ch
     * 获取当天的星期几
     */
    public static int getTodayWeek(Calendar now) {
        int week = now.get(Calendar.DAY_OF_WEEK);
        if (week == 1) {
            return 7;
        }
        return week - 1;
    }

    /**
     * 获取当天的星期几
     * @param millionSeconds 毫秒
     */
    public static int getTodayWeek(long millionSeconds) {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(millionSeconds);
        return getTodayWeek(cal);
    }

    /**
     * 获取当天的星期几
     */
    public static int getTodayWeek() {
        return getTodayWeek(Calendar.getInstance());
    }

    public static Date getDate(int year, int month, int day, int hour, int minute, int second) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.MILLISECOND, 0);
        cal.set(year, month, day, hour, minute, second);
        return cal.getTime();
    }

    //  是否同一个月
    public static boolean isSameMonth(long updateTime) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTimeInMillis(System.currentTimeMillis());

        Calendar cal2 = Calendar.getInstance();
        cal2.setTimeInMillis(updateTime);
        return cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) && cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH);
    }

    /**
     * 是否是相邻两天一天
     * @param smallTime 单位秒
     * @param bigTime 单位秒
     */
    public static boolean isNextDay(long smallTime, long bigTime) {
        return isSameDay(smallTime + DAY_SECONDS, bigTime);
    }

    /**
     * 是否是同一天
     * @param time1 单位秒
     * @param time2 单位秒
     */
    public static boolean isSameDay(long time1, long time2) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTimeInMillis(time1 * 1000);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTimeInMillis(time2 * 1000);
        return cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
                && cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH)
                && cal1.get(Calendar.DAY_OF_MONTH) == cal2.get(Calendar.DAY_OF_MONTH);
    }


    /**
     * 是否同一个月份
     * @param time1 单位秒
     * @param time2 单位秒
     */
    public static boolean isSameMonth(long time1, long time2) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTimeInMillis(time1 * 1000);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTimeInMillis(time2 * 1000);
        return cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) && cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH);
    }

    /**
     * 获取这周周一0点时间
     * @param date 某时间
     */
    public static Date getThisWeekMonday(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        // 获得当前日期是一个星期的第几天
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        // 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        // 获得当前日期是一个星期的第几天
        int day = cal.get(Calendar.DAY_OF_WEEK);
        // 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);

        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    /**
     * 获取下周周一0点时间
     * @param date 某时间
     */
    public static Date getNextWeekMonday(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getThisWeekMonday(date));
        cal.add(Calendar.DATE, 7);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    //定制間隔重置(代码写错了，这个方法永远都不要用了，小于7天重置就会出问题)
    public static Date getNextResetDate(Date date,int day) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getThisWeekMonday(date));
        cal.add(Calendar.DATE, day);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    public static boolean isValidQueryDate(String str) {
        boolean convertSuccess=true;
        // 指定日期格式为四位年/两位月份/两位日期，注意yyyy/MM/dd区分大小写；
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH");
        try {// 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期，比如2007/02/29会被接受，并转换成2007/03/01
            format.setLenient(false);
            format.parse(str);
        } catch (ParseException e) {
            // e.printStackTrace();
            // 如果throw java.text.ParseException或者NullPointerException，就说明格式不对
            convertSuccess=false;
        }
        return convertSuccess;
    }

    public static long getQueryDateTime(String str){
        // 指定日期格式为四位年/两位月份/两位日期，注意yyyy/MM/dd区分大小写；
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH");
        try {// 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期，比如2007/02/29会被接受，并转换成2007/03/01
            format.setLenient(false);
            Date date = format.parse(str);
            return date.getTime();
        } catch (ParseException e) {
            // e.printStackTrace();
            // 如果throw java.text.ParseException或者NullPointerException，就说明格式不对
            return 0;
        }
    }

    public static long str2timestamp(String time){
        long timeStamp = 0;
        SimpleDateFormat format =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date = format.parse(time);
            //日期转时间戳（毫秒）
            timeStamp = date.getTime();
        }catch (Exception e){

        }
        return timeStamp;
    }

    public static long str2timestamp(String time, String pattern){
        long timeStamp = 0;
        SimpleDateFormat format =  new SimpleDateFormat(pattern);
        try {
            Date date = format.parse(time);
            //日期转时间戳（毫秒）
            timeStamp = date.getTime();
        }catch (Exception e){

        }
        return timeStamp;
    }

    //获取时间戳的当日零点时间戳 毫秒
    public static long getZeroTimeStamp(long inTime){
        long todayStartTime = inTime - (inTime + TimeZone.getDefault().getRawOffset()) % (1000 * 3600 * 24);
        return todayStartTime;
    }

    /**
     * 获取时间戳的当日零点时间戳 s
     * @param inTime s
     * @return
     */
    public static int getZeroTime(int inTime){
        return (int) (getZeroTimeStamp(inTime * 1000L) / 1000);
    }


    /**
     * 获取今天走过的秒数
     * @return
     */
    public static int getTodaySecond(){
        int now = getCurrentSecond();
        int zeroTime = getZeroTime(now);
        return now - zeroTime;
    }


    /**
     * 获取当前日期与指定日期相差几天
     *
     * @param time
     *            指定日期的时间戳(s)
     * @return
     */
    public static long getDiffDayNum(long time) {
        long todayZeroTime = getTodayZeroTimeStamp() / 1000;
        if (time >= todayZeroTime) {
            // 注意 有可能为负数
            return (todayZeroTime - time) / DAY_SECONDS;
        }
        return (todayZeroTime - 1 - time) / DAY_SECONDS + 1;
    }


    /**
     * 根据当前日期返回 dateFormat 格式字符串
     * @param dateFormat
     * @return
     */
    public static String getCurrentDateTimeString(String dateFormat) {
        SimpleDateFormat sdf = TimeHelper.getSimpleDateFormat(dateFormat);
        return sdf.format(Calendar.getInstance().getTime());
    }

    /**
     * 获取本周一0点的时间戳 (s)
     */
    public static int getWeekBeginTimestamp() {
        long secs = getCurrentSecond();
        return getWeekBeginTimestamp(secs);
    }

    /**
     * 获取当前时间的周一0点的时间戳 (s)
     * @param time 要计算的时间 s
     * @return
     */
    public static int getWeekBeginTimestamp(long time) {
        return (int) (time - ((time + (TimeZone.getDefault().getRawOffset() / 1000)) % DAY_SECONDS) - ((getDays1970(time) + 3) % 7) * DAY_SECONDS);//0星期1...6星期日
    }

    /**
     * 通过当前秒数获取1970年至今的天数
     */
    public static int getDays1970(long timeSeconds) {
        return (int) ((timeSeconds + (TimeZone.getDefault().getRawOffset() / 1000)) / DAY_SECONDS);
    }

    public static long getNextMonthFirstDayTime() {
        long nowSecond = getCurrentSecond();
        if (nowSecond > nextMonthFirstDayTime) {
            nextMonthFirstDayTime = getNextMonthFirstDayZeroTime();
            currentMonthFirstDayTime = getMonthFirstDayZeroTime();
        }
        return nextMonthFirstDayTime;
    }

    public static long getCurrentMonthFirstDayTime() {
        long nowSecond = getCurrentSecond();
        if (nowSecond > nextMonthFirstDayTime) {
            nextMonthFirstDayTime = getNextMonthFirstDayZeroTime();
            currentMonthFirstDayTime = getMonthFirstDayZeroTime();
        }
        return currentMonthFirstDayTime;
    }

    /**
     * 获取当前周的0点时间 (指定周几0点为本周第一天)
     *
     * @param date 某时间
     * @param firstDayOfWeek Calendar枚举
     */
    public static Date getThisWeekFirstDay(Date date, int firstDayOfWeek) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), java.time.ZoneId.systemDefault());
        LocalDate localDate = localDateTime.toLocalDate().with(TemporalAdjusters.previousOrSame(convertToDayOfWeek(firstDayOfWeek)));
        localDateTime = localDate.atStartOfDay();
        return java.util.Date.from(localDateTime.atZone(java.time.ZoneId.systemDefault()).toInstant());
    }

    /**
     * 将 Calendar.DATE_OF_WEEK 常量值转换成 DayOfWeek 枚举类型
     *
     * @param calendarDayOfWeek Calendar.DATE_OF_WEEK 常量值
     * @return DayOfWeek 枚举类型
     */
    public static DayOfWeek convertToDayOfWeek(int calendarDayOfWeek) {
        switch (calendarDayOfWeek) {
            case Calendar.SUNDAY:
                return DayOfWeek.SUNDAY;
            case Calendar.MONDAY:
                return DayOfWeek.MONDAY;
            case Calendar.TUESDAY:
                return DayOfWeek.TUESDAY;
            case Calendar.WEDNESDAY:
                return DayOfWeek.WEDNESDAY;
            case Calendar.THURSDAY:
                return DayOfWeek.THURSDAY;
            case Calendar.FRIDAY:
                return DayOfWeek.FRIDAY;
            case Calendar.SATURDAY:
                return DayOfWeek.SATURDAY;
            default:
                throw new IllegalArgumentException("Invalid Calendar.DAY_OF_WEEK value: " + calendarDayOfWeek);
        }
    }

    /**
     * 将每周第几天（周一为第一天）转换成 Calendar.DAY_OF_WEEK 常量值
     *
     * @param dayOfWeek 每周第几天（周一为第一天）
     * @return Calendar.DAY_OF_WEEK 常量值
     */
    public static int convertToCalendarDayOfWeek(int dayOfWeek) {
        switch (dayOfWeek) {
            case 1:
                return Calendar.MONDAY;
            case 2:
                return Calendar.TUESDAY;
            case 3:
                return Calendar.WEDNESDAY;
            case 4:
                return Calendar.THURSDAY;
            case 5:
                return Calendar.FRIDAY;
            case 6:
                return Calendar.SATURDAY;
            case 7:
                return Calendar.SUNDAY;
            default:
                throw new IllegalArgumentException("Invalid dayOfWeek value: " + dayOfWeek);
        }
    }

    /**
     * 本周id
     */
    public static int getThisWeekId() {
        return getThisWeekId(1);
    }

    /**
     * 本周id，指定本周第一天
     */
    public static int getThisWeekId(int firstDayOfWeek) {
        return getThisWeekId(DateHelper.getCurrentSecond(), firstDayOfWeek);
    }

    /**
     * 上周id，指定本周第一天
     */
    public static int getPreWeekId(int firstDayOfWeek) {
        return getThisWeekId(DateHelper.getCurrentSecond() - DateHelper.WEEK_SECONDS, firstDayOfWeek);
    }

    /**
     * 本周id，指定time所在周的指定起始天
     *
     * @param time 要计算的时间 s
     */
    public static int getThisWeekId(long time, int firstDayOfWeek) {
        SimpleDateFormat sdf = TimeHelper.getSimpleDateFormat(YYMMDD);
        return Integer.parseInt(sdf.format(getThisWeekStartTime(time, firstDayOfWeek)));
    }

    /**
     *
     * @param time 秒
     * @param firstDayOfWeek 周几第一天
     * @return
     */
    public static long getThisWeekStartTime(long time, int firstDayOfWeek) {
        long weekBeginTimestamp = getWeekBeginTimestamp(time) * DateHelper.SECOND_MILLIONS;
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(time * DateHelper.SECOND_MILLIONS);
        int todayWeek = getTodayWeek(cal);
        if (todayWeek < firstDayOfWeek) {
            weekBeginTimestamp -= DateHelper.WEEK_SECONDS * DateHelper.SECOND_MILLIONS;
        }
        weekBeginTimestamp += (firstDayOfWeek - 1) * DateHelper.DAY_MILLIONS;
        return weekBeginTimestamp;
    }

    public static long getThisWeekStartTime(int firstDayOfWeek) {
        return getThisWeekStartTime(DateHelper.getCurrentSecond(), firstDayOfWeek);
    }

    /**
     * 时间戳转日期字符串
     * @param time
     * @param format
     * @return
     */
    public static String timeToDateStr(long time, String format) {
        Date date = new Date(time);
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    /**
     * 判断2时间戳是否是同一天
     * @param time1
     * @param time2
     * @return
     */
    public static boolean checkTimeOneDay(long time1, long time2) {
        String dateStr1 = timeToDateStr(time1, "yyyy-MM-dd");
        String dateStr2 = timeToDateStr(time2, "yyyy-MM-dd");
        if (dateStr1.equals(dateStr2)) {
            return true;
        }
        return false;
    }

    /**
    * 描述：获取时间格式整形数值，例如20231101
    * 作者：zrq
    * 时间：2023/11/1
    * 参数：
    时间，单位毫秒
    * 返回值：
    **/
    public static int getDateFormatDayId(long time) {
        SimpleDateFormat sdf = TimeHelper.getSimpleDateFormat(YYMMDD);
        return Integer.parseInt(sdf.format(time));
    }

    /**
     * 描述：获取当前时间的时间格式整形数值，例如20231101
     * 作者：zrq
     * 时间：2023/11/1
     * 参数：
     * 返回值：
     **/
    public static int getDateFormatDayId() {
        long nowTime = System.currentTimeMillis();
        return getDateFormatDayId(nowTime);
    }

    /**
    * 描述：获取时间小时的时间戳
    * 作者：zrq
    * 时间：2023/11/16
    * 参数：
    时间单位毫秒
    * 返回值：
    **/
    public static long getHoursTimeStamp(long time) {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(time);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTimeInMillis();
    }

    public static long getHoursTimeStamp() {
        long time = System.currentTimeMillis();
        return getHoursTimeStamp(time);
    }

    /**
    * 描述：获取指定时间的日期字符串，格式例如：2023-11-17
    * 作者：zrq
    * 时间：2023/11/17
    * 参数：
    时间单位毫秒
    * 返回值：
    **/
    public static String getYMDBARString(long time) {
        SimpleDateFormat sdf = TimeHelper.getSimpleDateFormat(YMD_BAR);
        return sdf.format(time);
    }

    /**
     * 获取n天后指定时间点的毫秒时间戳。
     *
     * @param daysLater n天后（n可以为负数表示前几天）
     * @param hour      小时（0-23）
     * @param minute    分钟（0-59）
     * @param second    秒（0-59）
     * @return n天后指定时间点的毫秒时间戳
     */
    public static long getTimestampNDaysLater(int daysLater, int hour, int minute, int second) {
        // 当前日期加上n天
        LocalDate date = LocalDate.now().plusDays(daysLater);
        // 指定时间
        LocalTime time = LocalTime.of(hour, minute, second);
        // 结合日期和时间
        LocalDateTime dateTime = LocalDateTime.of(date, time);
        // 转换为系统默认时区的ZonedDateTime
        ZonedDateTime zonedDateTime = dateTime.atZone(ZoneId.systemDefault());
        // 返回毫秒时间戳
        return zonedDateTime.toInstant().toEpochMilli();
    }

    /**
     * 计算前n天的日期时间戳。
     *
     * @param days 天数，
     * @return n天前的日期对应的时间戳（毫秒）
     */
    public static long getTimestampForDaysBefore(int days) {
        LocalDate pastDate = LocalDate.now().minusDays(Math.abs(days)); // 获取n天前的日期
        Instant instant = pastDate.atStartOfDay(ZoneId.systemDefault()).toInstant(); // 转换为Instant
        return instant.toEpochMilli(); // 转换为毫秒时间戳
    }

    public static void main(String[] args) {
        {
            long time1 = DateHelper.str2timestamp("2025-01-01 01:00:00");
            long time2 = DateHelper.str2timestamp("2025-01-02 01:00:00");
            boolean sameWeek = isSameWeek(time1/1000, time2/1000);
            System.out.println("是不是同周"+sameWeek);
        }

        {
            long time1 = DateHelper.str2timestamp("2024-12-31 01:00:00");
            long time2 = DateHelper.str2timestamp("2025-01-01 01:00:00");
            boolean sameWeek = isSameWeek(time1/1000, time2/1000);
            System.out.println("是不是同周"+sameWeek);
        }
    }
}
